package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.Iterator;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.ImgRecordDetailActivity;
import flc.ast.adapter.TransferRecordAdapter;
import flc.ast.adapter.PictureRecordAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferRecordBean;
import flc.ast.databinding.FragmentPictureRecordBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PictureRecordFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PictureRecordFragment extends BaseNoModelFragment<FragmentPictureRecordBinding> {

    private boolean isSend;
    private boolean isEdit;
    private boolean isAllSel = false;
    private TransferRecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_picture_record;
    }

    public static PictureRecordFragment newInstance(boolean type) {
        Bundle args = new Bundle();
        args.putSerializable(Content.TRANSFER_TYPE, type);
        PictureRecordFragment fragment = new PictureRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        mDataBinding.llAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recordAdapter = new TransferRecordAdapter();
        mDataBinding.rvPictureList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llAllSel://全选
                if (isAllSel) {
                    List<TransferRecordBean> list = recordAdapter.getValidData();
                    for (TransferRecordBean bean : list) {
                        List<SelectFileBean> listKind = bean.getList();
                        for (SelectFileBean selectFileBean : listKind) {
                            selectFileBean.setSelected(false);
                        }
                    }
                    isAllSel = false;
                } else {
                    List<TransferRecordBean> list = recordAdapter.getValidData();
                    for (TransferRecordBean bean : list) {
                        List<SelectFileBean> listKind = bean.getList();
                        for (SelectFileBean selectFileBean : listKind) {
                            selectFileBean.setSelected(true);
                        }
                    }
                    isAllSel = true;
                }
                recordAdapter.notifyDataSetChanged();
                break;
            case R.id.tvDelete:
                isEdit = false;
                recordAdapter.setEdit(isEdit);
                List<TransferRecordBean> list = recordAdapter.getValidData();
                for (int i = 0; i < list.size(); i++) {
                    TransferRecordBean transferRecordBean = list.get(i);
                    List<SelectFileBean> listKind = transferRecordBean.getList();
                    Iterator<SelectFileBean> iterator = listKind.iterator();
                    while (iterator.hasNext()) {
                        SelectFileBean bean = iterator.next();
                        if (bean.isSelected()) {
                            iterator.remove();
                        }
                    }
                    if (listKind == null || listKind.size() == 0) {
                        list.remove(i);
                    }
                }
                if (isSend) {
                    AppSpUtils.saveTransferPicList(list);
                    getSendData();
                } else {
                    AppSpUtils.saveReceivePicList(list);
                    getReceiveData();
                }
                mDataBinding.llDelete.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof PictureRecordAdapter) {
            PictureRecordAdapter kindAdapter = (PictureRecordAdapter) adapter;
            SelectFileBean item = kindAdapter.getItem(position);
            if (isEdit) {
                if (item.isSelected()) {
                    item.setSelected(false);
                } else {
                    item.setSelected(true);
                }
                kindAdapter.notifyDataSetChanged();
            } else {
                ImgRecordDetailActivity.imgPath = item.getPath();
                startActivity(ImgRecordDetailActivity.class);
            }
        } else if (adapter == recordAdapter) {
            ToastUtils.showShort("111");
        }
    }

    @Override
    protected void initData() {
        isSend = getArguments().getBoolean(Content.TRANSFER_TYPE);
        if (isSend) {
            getSendData();
        } else {
            getReceiveData();
        }
    }

    //获取传送记录
    private void getSendData() {
        List<TransferRecordBean> listSp = AppSpUtils.getTransferPicList();
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.ivPictureNotData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.ivPictureNotData.setVisibility(View.VISIBLE);
        }
    }

    //获取接收记录
    private void getReceiveData() {
        List<TransferRecordBean> listSp = AppSpUtils.getReceivePicList();
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.ivPictureNotData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.ivPictureNotData.setVisibility(View.VISIBLE);
        }
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        if (isEdit) {
            mDataBinding.llDelete.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.llDelete.setVisibility(View.GONE);
        }
        recordAdapter.setEdit(isEdit);
        if (isSend) {
            getSendData();
        } else {
            getReceiveData();
        }
    }

}